package com.x.erp.dao;

import com.x.erp.model.Rotation;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class RotationDao extends BaseDao{

    /**
     * 轮播图列表
     * @param rotation
     * @return
     */
    private final static String selectRotationListSql = "SELECT T1.*, T2.MARKET_NAME FROM CMS_ROTATION T1, ERP_MARKET T2 WHERE T1.MARKET_ID = T2.MARKET_ID ";
    public List<Rotation> selectRotationList(Rotation rotation) throws Exception {
        StringBuilder sql = new StringBuilder(selectRotationListSql);
        List<Object> params = new ArrayList<Object>();
        searchCondition(sql, params, rotation);
        sql.append("ORDER BY T1.ROTATION_SEQ ASC ");
        return this.queryForList(sql.toString(), Rotation.class, params, rotation);
    }

    /**
     * 轮播图数量
     * @param rotation
     * @return
     */
    private final static String selectRotationListNunSql = "SELECT COUNT(T1.ROTATION_ID) FROM CMS_ROTATION T1, ERP_MARKET T2 WHERE T1.MARKET_ID = T2.MARKET_ID ";
    public Integer selectRotationListNum(Rotation rotation) throws Exception {
        StringBuilder sql = new StringBuilder(selectRotationListNunSql);
        List<Object> params = new ArrayList<Object>();
        searchCondition(sql, params, rotation);
        return this.queryForInt(sql.toString(), params);
    }

    /**
     * 查询条件
     * @param sql
     * @param params
     * @param rotation
     */
    private void searchCondition(StringBuilder sql, List<Object> params, Rotation rotation) {
        if(rotation != null) {
            if (this.isString(rotation.getRotationId())) {
                sql.append("AND T1.ROTATION_ID = ? ");
                params.add(rotation.getRotationId());
            }
            if (this.isString(rotation.getRotationName())) {
                sql.append("AND T1.ROTATION_NAME = ? ");
                params.add(rotation.getRotationName());
            }
            if (this.isNotNull(rotation.getRotationShowSeconds())) {
                sql.append("AND T1.ROTATION_SHOW_SECONDS = ? ");
                params.add(rotation.getRotationShowSeconds());
            }
            if (this.isNotNull(rotation.getRotationState())) {
                sql.append("AND T1.ROTATION_STATE = ? ");
                params.add(rotation.getRotationState());
            }
            if (this.isString(rotation.getMarketId())) {
                sql.append("AND T1.MARKET_ID = ? ");
                params.add(rotation.getMarketId());
            }
            if (this.isString(rotation.getUserId())) {
                sql.append("AND T1.USER_ID = ? ");
                params.add(rotation.getUserId());
            }
        }
    }
}
